﻿using System;
using System.ComponentModel;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using Model.Interfaces;

namespace Data.MappingClasses
{
    [TableAttribute(Name = "dbo.Subject")]
    public partial class Subject : AbstractMappingClass, INotifyPropertyChanging, INotifyPropertyChanged
    {

        private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);

        private short _idSubject;

        private string _nameSubject;

        private string _shortNameSubject;

        private EntitySet<TeacherSubject> _TeacherSubjects;

        #region Extensibility Method Definitions
        partial void OnLoaded();
        partial void OnValidate(ChangeAction action);
        partial void OnCreated();
        partial void OnidSubjectChanging(short value);
        partial void OnidSubjectChanged();
        partial void OnnameSubjectChanging(string value);
        partial void OnnameSubjectChanged();
        partial void OnshortNameSubjectChanging(string value);
        partial void OnshortNameSubjectChanged();
        #endregion

        public Subject()
        {
            this._TeacherSubjects = new EntitySet<TeacherSubject>(AttachTeacherSubjects, DetachTeacherSubjects);
            OnCreated();
        }

        [ColumnAttribute(Storage = "_idSubject", AutoSync = AutoSync.OnInsert, DbType = "SmallInt NOT NULL IDENTITY", IsPrimaryKey = true, IsDbGenerated = true)]
        public short idSubject
        {
            get
            {
                return this._idSubject;
            }
            set
            {
                if ((this._idSubject != value))
                {
                    this.OnidSubjectChanging(value);
                    this.SendPropertyChanging();
                    this._idSubject = value;
                    this.SendPropertyChanged("idSubject");
                    this.OnidSubjectChanged();
                }
            }
        }

        [ColumnAttribute(Storage = "_nameSubject", DbType = "VarChar(75) NOT NULL", CanBeNull = false)]
        public string nameSubject
        {
            get
            {
                return this._nameSubject;
            }
            set
            {
                if ((this._nameSubject != value))
                {
                    this.OnnameSubjectChanging(value);
                    this.SendPropertyChanging();
                    this._nameSubject = value;
                    this.SendPropertyChanged("nameSubject");
                    this.OnnameSubjectChanged();
                }
            }
        }

        [ColumnAttribute(Storage = "_shortNameSubject", DbType = "VarChar(25) NOT NULL", CanBeNull = false)]
        public string shortNameSubject
        {
            get
            {
                return this._shortNameSubject;
            }
            set
            {
                if ((this._shortNameSubject != value))
                {
                    this.OnshortNameSubjectChanging(value);
                    this.SendPropertyChanging();
                    this._shortNameSubject = value;
                    this.SendPropertyChanged("shortNameSubject");
                    this.OnshortNameSubjectChanged();
                }
            }
        }

        [AssociationAttribute(Name = "Subject_TeacherSubject", Storage = "_TeacherSubjects", ThisKey = "idSubject", OtherKey = "refSubject")]
        public EntitySet<TeacherSubject> TeacherSubjects
        {
            get
            {
                return this._TeacherSubjects;
            }
            set
            {
                this._TeacherSubjects.Assign(value);
            }
        }

        public event PropertyChangingEventHandler PropertyChanging;

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void SendPropertyChanging()
        {
            if ((this.PropertyChanging != null))
            {
                this.PropertyChanging(this, emptyChangingEventArgs);
            }
        }

        protected virtual void SendPropertyChanged(String propertyName)
        {
            if ((this.PropertyChanged != null))
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private void AttachTeacherSubjects(TeacherSubject entity)
        {
            this.SendPropertyChanging();
            entity.Subject = this;
        }

        private void DetachTeacherSubjects(TeacherSubject entity)
        {
            this.SendPropertyChanging();
            entity.Subject = null;
        }

        public override Model.ModelClasses.AbstractModel PureModel
        {
            get
            {
                return base.PureModel;
            }
            set
            {
                if (_pureModel != null)
                    _pureModel.UpdateModelEvent -= UpdateModelEventHandler;

                if (value != null)
                    value.UpdateModelEvent += UpdateModelEventHandler;

                _pureModel = value;
            }
        }

        private void UpdateModelEventHandler(object sender, EventArgs e)
        {
            nameSubject = ((ISubject) _pureModel).Name;
            UpdateData();
        }
    }
}